package com.service.calculate.hookArmCase;

import com.service.bean.data.Data;
import com.service.bean.signal.GpsHookArmCaseSignal;

/**
 * 状态位、开关量、故障位、输出位解析
 * @author lj
 *
 */
public class HookArmCaseStatusCalculator {
	/**
	 * 状态位
	 */
	private long rawStatus;
	/**
	 * 开关量
	 */
	@SuppressWarnings("unused")
	private long rawSwitch;
	/**
	 * 故障位
	 */
	private long rawFault;
	/**
	 * 输出位
	 */
	@SuppressWarnings("unused")
	private long rawOutput;
	/**
	 * 0: 无效数据</br>1: 有效数据
	 */
	private int gpsStatus;
	/**
	 * 0:历史数据</br>1: 实时数据
	 */
	private int dataTypes;
	/**
	 * 0: 勾臂车箱体未满</br>1: 勾臂车箱体已满
	 */
	private int isGarbageFull;
	
	/**
	* 电源状态
	* 0：未接电源充电
	* 1：已接电源充电
	*/
	private int batteryStatus;
	
	public void calculate(Data data,GpsHookArmCaseSignal signal) {
		init(data);
		signal.gpsStatus = gpsStatus;
		signal.dataTypes = dataTypes;
		signal.isGarbageFull = isGarbageFull;
		signal.batteryStatus = batteryStatus;
	}
	
	private void init(Data data) {
		rawStatus = Long.parseLong(data.rawStatus, 16);
		rawSwitch = Long.parseLong(data.rawSwitch, 16);
		rawFault = Long.parseLong(data.rawFault, 16);
		rawOutput = Long.parseLong(data.rawOutput, 16);
		
		gpsStatus = indexOfByte(this.rawStatus,0);
		dataTypes = indexOfByte(this.rawStatus,4);
		isGarbageFull = indexOfByte(this.rawFault,18);
		batteryStatus = indexOfByte(this.rawStatus,3);
	}
	
	private int indexOfByte(long val, int index) {
		if (((byte) (val >> index) & 0x01) > 0) {
			return 1;
		} else {
			return 0;
		}
	}
	
}
